<?php
App::uses('AppController', 'Controller');
/**
 * Suppliers Controller
 *
 * @property Supplier $Supplier
 */
class SuppliersController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Supplier->recursive = 0;
		$this->set('suppliers', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Supplier->exists($id)) {
			throw new NotFoundException(__('Invalid supplier'));
		}
		$options = array('conditions' => array('Supplier.' . $this->Supplier->primaryKey => $id));
		$this->set('supplier', $this->Supplier->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Supplier->create();
			$result = $this->Supplier->save($this->request->data);
			if ($result) {
			
			// if ajax request, then back to json data
            if ($this->request->is ( "ajax" )) {
                header ( "Pragma: no-cache" );
                header ( "Cache-Control: no-store, no-cache, max-age=0, must-revalidate" );
                header ( 'Content-Type: text/x-json' );
                header ( 'Content-type: application/json' );
                $supplierlist = array ();
                $status=false;

                $supplier_all = $this->Supplier->find ( "all" );
						
				if (! empty ( $supplier_all ))
				{
					foreach ( $supplier_all as $v ) {$supplierlist[]=$v["Supplier"];}						
				}
				
                $status=true;
				
			   $msg = array (
					__ ( 'The Supplier has been saved' ),
					__ ( 'The Supplier could not be saved. Please, try again.' )
				);
                $data = array (
                    "status" => $status,
                    "message" => $status ? $msg [0] : $msg [1],
                    "supplierlist" => $supplierlist
                ); 
				
                echo json_encode ( $data );
                die ();
				
            } elseif ($result) {
                $this->Session->setFlash ( $msg [0] );
                $this->redirect ( array (
                    'action' => 'index'
                ) );
            } else {
                $this->Session->setFlash ( $msg [1] );
            }
			
			
				$this->Session->setFlash(__('The supplier has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The supplier could not be saved. Please, try again.'));
			}
		}
		
		$statuses = $this->Supplier->Status->find('list');
		$this->set(compact('statuses'));
		
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Supplier->exists($id)) {
			throw new NotFoundException(__('Invalid supplier'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Supplier->save($this->request->data)) {
				$this->Session->setFlash(__('The supplier has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The supplier could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Supplier.' . $this->Supplier->primaryKey => $id));
			$this->request->data = $this->Supplier->find('first', $options);
		}
		$statuses = $this->Supplier->Status->find('list');
		$this->set(compact('statuses'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Supplier->id = $id;
		if (!$this->Supplier->exists()) {
			throw new NotFoundException(__('Invalid supplier'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Supplier->delete()) {
			$this->Session->setFlash(__('Supplier deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Supplier was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
